package com.yjxxt.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

// mybatis工具类   提供 获取SqlSessionFactory以及SqlSession对象
public class MybatisUtils {
    static SqlSessionFactory ssf = null;
    static{

        try {
            String src = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(src);
             ssf= new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 获取SqlSession
    public static SqlSession getSqlSession(){
        return ssf.openSession(true); //设置自动提交
    }
}
